<template>
  <div class="interest-selection">
    <!-- 页面头部 -->
    <div class="header">
      <h1 class="title">选择你的兴趣，发现更多精彩</h1>
      <p class="subtitle">选择你感兴趣的内容，我们会为你推荐更精准的内容</p>
    </div>

    <!-- 标签网格 -->
    <div class="tags-container">
      <div class="tags-grid">
        <TagCard
          v-for="tag in tags"
          :key="tag.id"
          :tag="tag"
          :selected="selectedTags.includes(tag.id)"
          @click="toggleTag(tag.id)"
        />
      </div>
    </div>

    <!-- 选择提示 -->
    <div class="selection-info">
      <p class="selection-count">
        已选择 {{ selectedTags.length }}/5 个兴趣
      </p>
      <p v-if="selectedTags.length > 5" class="error-message">
        最多选择 5 个兴趣哦
      </p>
    </div>

    <!-- 底部按钮 -->
    <div class="bottom-actions">
      <button
        class="submit-btn"
        :disabled="selectedTags.length === 0 || selectedTags.length > 5 || isSubmitting"
        @click="submitTags"
      >
        <span v-if="isSubmitting" class="loading-spinner"></span>
        {{ isSubmitting ? '提交中...' : '我选好了' }}
        <span class="lightning-icon">⚡</span>
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import TagCard from '../components/TagCard.vue'
import { tagAPI, userAPI } from '../api'

const router = useRouter()

// 响应式数据
const tags = ref([])
const selectedTags = ref([])
const isSubmitting = ref(false)

// 获取标签列表
const fetchTags = async () => {
  try {
    const response = await tagAPI.getAllTags()
    tags.value = response.data
  } catch (error) {
    console.error('获取标签失败:', error)
  }
}

// 切换标签选择状态
const toggleTag = (tagId) => {
  const index = selectedTags.value.indexOf(tagId)
  if (index > -1) {
    // 已选中，取消选择
    selectedTags.value.splice(index, 1)
  } else {
    // 未选中，添加选择
    if (selectedTags.value.length < 5) {
      selectedTags.value.push(tagId)
    } else {
      // 超过5个，提示用户
      console.log('最多选择5个兴趣')
    }
  }
}

// 提交标签选择
const submitTags = async () => {
  if (selectedTags.value.length === 0 || selectedTags.value.length > 5) {
    return
  }

  isSubmitting.value = true

  try {
    const response = await userAPI.setUserTags(selectedTags.value)

    if (response.data.code === 0) {
      // 提交成功，跳转到首页
      router.push('/home')
    } else {
      console.error('提交失败:', response.data.msg)
    }
  } catch (error) {
    console.error('提交标签失败:', error)
    if (error.response?.status === 401) {
      // Token无效，跳转到登录页
      localStorage.removeItem('token')
      localStorage.removeItem('user')
      router.push('/login')
    }
  } finally {
    isSubmitting.value = false
  }
}

// 组件挂载时获取标签
onMounted(() => {
  fetchTags()
})
</script>

<style>
/* 全局样式，确保全屏显示 */
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow-x: hidden;
}

#app {
  height: 100%;
}
</style>

<style scoped>
.interest-selection {
  height: 100vh;
  width: 100vw;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
}

.header {
  text-align: center;
  padding: 60px 20px 40px;
  background: linear-gradient(135deg, #ff6b9d 0%, #c44569 100%);
  color: white;
  position: relative;
}

.header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #ff6b9d, #c44569, #ff6b9d);
}

.title {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 12px;
  letter-spacing: -0.5px;
}

.subtitle {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
  font-weight: 400;
}

.tags-container {
  flex: 1;
  padding: 40px 20px;
  background: #ffffff;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 0;
}

.tags-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  width: 100%;
  max-width: 500px;
  justify-items: center;
}

.selection-info {
  text-align: center;
  padding: 20px;
  background: #f8f9fa;
  border-top: 1px solid #e9ecef;
}

.selection-count {
  font-size: 16px;
  margin: 0 0 8px 0;
  font-weight: 600;
  color: #495057;
}

.error-message {
  color: #dc3545;
  font-size: 14px;
  margin: 0;
  font-weight: 500;
}

.bottom-actions {
  padding: 20px;
  background: #ffffff;
  border-top: 1px solid #e9ecef;
  position: sticky;
  bottom: 0;
  z-index: 10;
}

.submit-btn {
  width: 100%;
  height: 56px;
  background: linear-gradient(135deg, #ff6b9d 0%, #c44569 100%);
  color: white;
  border: none;
  border-radius: 28px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  box-shadow: 0 4px 20px rgba(255, 107, 157, 0.3);
}

.submit-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(255, 107, 157, 0.4);
}

.submit-btn:active:not(:disabled) {
  transform: translateY(0);
}

.submit-btn:disabled {
  background: #dee2e6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.lightning-icon {
  font-size: 16px;
  opacity: 0.8;
}

.loading-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid transparent;
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .tags-grid {
    grid-template-columns: repeat(3, 1fr);
    max-width: 400px;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 40px 20px 30px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .subtitle {
    font-size: 14px;
  }
  
  .tags-container {
    padding: 30px 20px;
  }
  
  .tags-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
    max-width: 350px;
  }
  
  .bottom-actions {
    padding: 16px;
  }
  
  .submit-btn {
    height: 50px;
    font-size: 16px;
  }
}

@media (max-width: 480px) {
  .tags-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    max-width: 280px;
  }
  
  .header {
    padding: 30px 16px 20px;
  }
  
  .title {
    font-size: 20px;
  }
  
  .tags-container {
    padding: 20px 16px;
  }
}

@media (min-width: 1200px) {
  .tags-grid {
    max-width: 600px;
  }
}
</style>
